/* 
 * File:   Application.cpp
 * Author: hanv
 * 
 * Created on September 29, 2013, 4:26 PM
 */

#include "Application.h"
#include "SimpleNumberParser.h"
#include <string>
#include <iostream>
#include <stdlib.h>

using namespace std;

Application* Application::m_Instance = NULL;

Application::Application() {}

Application::~Application() {}

int Application::run(int argc, char** argv){
    /*
     * cases:
     * 
     * (1) show help
     * * parameters: --help (or -h); any incorrect parameters
     * 
     * (2) parse 1 number
     * * parameters: 
     * * (a) --number <number> (or -n <number>)
     * * (b) --language <language> (or -l <language>) OR --format <format config file> (or -f <format config file>)
     * 
     * (3) start application to parse many numbers
     * * parameters: none OR --start (or -s) 
     */    
    RunType runType = HELP;
    
    if (argc == 1) {
        runType = MANY;
    } else if (argc == 2) {
        if ((argv[1] == "--help") || (argv[1] == "-h")){
//            runType = HELP;
        } else  if ((argv[1] == "--start") || (argv[1] == "-s")){
            runType = MANY;
        } 
//        else {
//            runType = HELP;
//        }
    } 
//    else {
//            runType = HELP;
//    }
    
    switch (runType){
        case MANY:
        {
            runLoop();
            return 0;            
        }        
        case ONCE_LANG:
        case ONCE_CONF:
        case HELP:
        default:
            showHelp();   
            return 0;
    }
    
    /*
     * application commands:
     * (1) help
     * (2) any number
     * (3) load language <language>
     * (4) load format <format config file>
     * (5) exit OR quit OR bye
     */
}

Application* Application::instance(){
    if (m_Instance == NULL){
        m_Instance = new Application();
    }
    return m_Instance;
}

void Application::clean(){
    if (m_Instance != NULL){
        delete m_Instance;
    }
}

void Application::showHelp(){
    cout << "Usage:" << endl;
    cout << "\t 1. --help OR -h for help" << endl;
    cout << "\t 2. no parameter OR --start OR -s to start" << endl;
    cout << "\t 3. --number <number> OR -n <number> to print only <number>" << endl;
    cout << "\t 3b. use --number <number> and --language <language> OR -format <format file> to print <number> in another language or format to default" << endl;
}

void Application::runLoop(){
    while (true){
        string input;
        cin >> input;
        bool error = false;
        long number = SimpleNumberParser::parseLong(input,error);
        if (number != 0){
            cout << "number" << number << endl;
            cout << "error" << error << endl;
        } else {
            cout << "number" << number << endl;
            cout << "error" << error << endl;
            if (input == "exit"){
                return;
            }
        }
    }
}